<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
    {% load static %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .account {
            width: 400px;
            margin: 0 auto;
        }
    </style>
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Tracer</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a href="#">产品功能</a></li>
                <li><a href="#">企业方案</a></li>
                <li><a href="#">帮助文档</a></li>
                <li><a href="#">价格</a></li>

            </ul>

            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>
<div class="account">
    <div class="title">用户注册</div>
    <form action="" method="post" id="registerForm" novalidate enctype="multipart/form-data">
        {% csrf_token %}
        {% for field in form_obj %}
            {% if field.name == 'code' %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">
                        {% if field.errors %}{{ field.errors.0 }}{% else %}{{ field.label }}{% endif %}
                    </label>
                    <div class="row">
                        <div class="col-xs-7">{{ field }}</div>
                        <div class="col-xs-5">
                            <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                        </div>
                    </div>

                </div>
            {% else %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">

                        {% if field.errors %}{{ field.errors.0 }}{% else %}{{ field.label }}{% endif %}
                        {{ field }}

                    </label>

                </div>
            {% endif %}
        {% endfor %}

        <button type="submit" class="btn btn-primary">注 册</button>
    </form>

</div>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    $(function () {

        bindClickBtnSms();
    });
    $('#btnSms').click(function () {
        var mobile_phone = $('#mobile_phone').val();

        $.ajax({
            url: '/app01/code_phone/',
            data: {
                mobile_phone: mobile_phone,

            },
            success: (res) => {
                if (res.status) {
                    sendSmsRemind();
                } else {
                    alert('请检查手机号')
                }
            }

        })
    })

    /*
倒计时
 */
    function sendSmsRemind() {
        var $smsBtn = $('#btnSms');
        $smsBtn.prop('disabled', true); // 禁用
        var time = 60;
        var remind = setInterval(function () {
            $smsBtn.val(time + '秒重新发送');
            time = time - 1;
            if (time < 1) {
                clearInterval(remind);
                $smsBtn.val('点击获取验证码').prop('disabled', false);
            }
        }, 1000)

    }
</script>
</body>
</html>